﻿function CheckNumber(valuecheck,retmessage)
{
	if(isNaN(valuecheck)){
		alert(retmessage);
		return false;
	}
	return true;
}

function ReloadInfoCart(){	
	var siteid = document.getElementById("hdsiteid").value;
	var RetValue = Portal.AppBLL.RenderShoppingCart(siteid).value;
	document.getElementById("shoppingcart").innerHTML = RetValue;
}

function OnclientUpdateCart(){	
  var txtArray = document.getElementById("dtcart").getElementsByTagName("td");//replace div11 with the id of your div
  var arrayVal = "";
  var error = "";
	for (var co = 0; co < txtArray.length; co++) {
		var inputArray = txtArray[co].getElementsByTagName("input");
		if(inputArray.length >0)
		{
			for (var inum = 0; inum < inputArray.length; inum++) 
			{
				if(inputArray[inum].id.indexOf("txt_") != -1)
				{
					var itemVal = inputArray[inum].value;
					
					if(itemVal =="0" || itemVal.length > 2 || isNaN(itemVal) == true)
					{
						error = "1";
						alert("Số lượng sản phẩm không hợp lệ");
						break;
					}
					arrayVal +=inputArray[inum].id + ";" + inputArray[inum].value + "|";
				}
			}
		}
		if(error.length >0)
		{ 
			break;
		}
	}
	if(error.length == 0)
	{
		var siteid = document.getElementById("hdsiteid").value;
		var RetValue = Portal.AppBLL.GetIdtemToCart(siteid,arrayVal,'0',2).value;
		document.getElementById("divcartholder").innerHTML = RetValue;
	}
}
function OnclientPrintCart(prefixUrl) {
        var pageURL = prefixUrl + "dialog/print.aspx";
        targetWin = window.open(pageURL,'Print Order','toolbar=no, location=no, directories=no, status=no, menubar=no');
        targetWin.focus();
        return;
}
function OnclientSubmitCart(prefixurl, isLogin,loginName) {
    if (isLogin == 'True') {
        var bodyVal = "<table class='carttable' id='dtcart'>" + document.getElementById("dtcart").innerHTML + "</table>";
        var RetValue = BLL.CmsBLL.AddCardOrder1(loginName, bodyVal).value;

        if (RetValue.indexOf("ERROR:") != -1) {
            alert("Sent order fail, please come back!");
            return;
        } else {
            alert("Sent order successed,Thanks you!");
            window.location.href = prefixurl + "Home.html";
        }
    } else {
                var pageURL = prefixurl + "dialog/customer.aspx";
                var title = "Nhập thông tin khách hàng";
                var w = "500";
                var h = "500";
                var left = (screen.width / 2) - (w / 2);
                var top = (screen.height / 2) - (h / 2) - 50;
                targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
                targetWin.focus();
                return;
    }
}
function RealOnclientSubmitCart() {
    var nameVal = new String(document.getElementById("txtUserName").value);
    if (nameVal.length == 0) {
        alert("Vui lòng nhập họ tên!");
        document.getElementById("txtUserName").focus();
        return;
    }
    var emailVal = new String(document.getElementById("txtEmail").value);
    if (emailVal.length == 0) {
        alert("Vui lòng nhập địa chỉ email!");
        document.getElementById("txtEmail").focus();
        return;
    }
    //var reg = /^(([a-zA-Z])+(([a-zA-Z0-9])*(\_{1})*([a-zA-Z])+)+\.)*(([a-zA-Z])+(([a-zA-Z0-9])+)+)+@(([a-zA-Z])+(([a-zA-Z0-9])*(\-{1})*([a-zA-Z])+)+\.)/;
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(emailVal) == false) {
        alert("Email của bạn không hợp lệ!")
        document.getElementById("txtEmail").focus();
        return false;
    }
    var phoneVal = new String(document.getElementById("txtPhone").value);
    if (phoneVal.length == 0) {
        alert("Vui lòng số điện thoại!");
        document.getElementById("txtPhone").focus();
        return;
    }
    var bodyVal = "<table class='carttable' id='dtcart'>" + document.getElementById("dtcart").innerHTML + "</table>";
    var RetValue = BLL.CmsBLL.AddCardOrder(bodyVal, nameVal, emailVal, phoneVal).value;
    if (RetValue.indexOf("ERROR:") == -1) {
        //        document.getElementById("customer_info").innerHTML = "Cảm ơn bạn đã gửi đơn hàng, chúng tôi sẽ phản hồi trong thời gian sớm nhất";
        alert("Sent order successed,Thanks you!");
        document.getElementById("btnSubmitCart").style.display='none';
    }
    else {
        alert("Sent order fail, please come back!");
    }
}

function RemoveCartItem(pid){
	var siteid = document.getElementById("hdsiteid").value;
	var RetValue = Portal.AppBLL.GetIdtemToCart(siteid, pid,'1',3).value;
	if(RetValue.indexOf("ERROR:") == -1)
	{
		document.getElementById("divcartholder").innerHTML = RetValue;
	}
	ReloadInfoCart();
}
function addToShoppingCart(httproot,pid){
	var siteid = document.getElementById("hdsiteid").value;
	var RetValue = Portal.AppBLL.GetIdtemToCart(siteid, pid,'1',1).value;
	if(RetValue.indexOf("ERROR:") == -1)
	{
		window.location.href = httproot+"cart.html";
	}
}
//add product to card
function ClientBuyProduct(httproot,pid) {
    var RetValue = Portal.AppBLL.ServerBuyProduct(pid).value;
    if (RetValue.indexOf("ERROR:") == -1) {
        window.location.href = httproot +"cart.html";
        }
}
function closeformonbrowse(httproot) {
    var RetValue = BLL.CmsBLL.CheckSessionCart().value;
    if (RetValue.indexOf("1") == -1) {
        window.opener.location.href = httproot + "home.html";
        window.close();
        return;
    }
    else
        window.close();
}
function DoClose() {
    window.close();
}
function ClientGetProductsByMenu(pid,catid) {

    var RetValue = BLL.CmsBLL.RenderNewProductsByMenu('' + pid, '' + catid).value;
    if (RetValue.indexOf("ERROR:") == -1) {
        document.getElementById("divNewProducts").innerHTML = RetValue;
    }
}
function ClientGetDiscountProductsByMenu(pid, catid) {

    var RetValue = BLL.CmsBLL.RenderKhuyenMaiProductsByMenu('' + pid, '' + catid).value;
    
    if (RetValue.indexOf("ERROR:") == -1) {
        document.getElementById("divDiscountProducts").innerHTML = RetValue;
    }
}
